<template>
  <div class="bg-style">
    <i-pageBody class="add_i-color">
      <div slot="page-main">
        <div class="container container-space container-space--vertical">
          <div class="container-space__child">
            <Form ref="form" :model="formdata" :label-width="100">
              <Row :gutter="20">
                <i-col :span="5">
                  <FormItem prop="username" label="用户名称：">
                    <Select
                      v-model="formdata.username"
                      placeholder="请选择用户名称"
                      style="100%"
                      clearable
                    >
                      <Option
                        :value="value"
                        v-for="({ label, value }, index) in options.custs"
                        :key="index"
                        >{{ label }}</Option
                      >
                    </Select>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="device" label="设备类型：">
                    <Select
                      v-model="formdata.device"
                      placeholder="请选择设备类型"
                      style="100%"
                      clearable
                    >
                      <Option
                        :value="value"
                        v-for="({ label, value }, index) in options.equipments"
                        :key="index"
                        >{{ label }}</Option
                      >
                    </Select>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="deviceid" label="设备ID：">
                    <Input
                      type="text"
                      v-model="formdata.deviceid"
                      placeholder="请输入设备ID"
                      clearable
                    />
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="time" label="操作时间：">
                    <Date-picker
                      style="width:100%"
                      type="daterange"
                      placeholder="选择操作时间"
                      v-model="formdata.time"
                      clearable
                    ></Date-picker>
                  </FormItem>
                </i-col>
                <i-col :span="5">
                  <FormItem prop="account" label="操作账号：">
                    <Input
                      type="text"
                      v-model="formdata.account"
                      placeholder="请输入操作账号"
                      clearable
                    />
                  </FormItem>
                </i-col>
                <i-col :span="4">
                  <Button
                    type="primary"
                    @click="handleQuery"
                    :loading="loading.search"
                  >
                    查询
                  </Button>
                  <Button
                    type="default"
                    @click="handleReset"
                    :loading="loading.reset"
                  >
                    重置
                  </Button>
                </i-col>
              </Row>
            </Form>
          </div>
          <div class="container-space__child">
            <Table
              :columns="columns"
              :data="data"
              border
              stripe
              :loading="tableLoading"
            ></Table>
          </div>
          <div class="container-space__child" v-show="data.length > 0">
            <div class="container-page">
              <PageHeader
                :total="formdata.total"
                :page-size="formdata.pageSize"
                :page="formdata.page"
              ></PageHeader>
              <Page
                :total="formdata.total"
                :page-size="formdata.pageSize"
                :current="formdata.page"
                @on-change="handlePage"
                show-sizer
                @on-page-size-change="handlePageSize"
              />
            </div>
          </div>
        </div>
      </div>
    </i-pageBody>
    <Modal v-model="inversion.modal" :width="1200" title="控制列表">
      <div class="container-space container-space--vertical">
        <div class="container-space__child">
          <Table
            :columns="inversion.columns"
            :data="inversion.data"
            border
            stripe
          ></Table>
        </div>
      </div>
      <div slot="footer">
        <Button type="default" @click="inversion.modal = false">
          关闭
        </Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import PageHeader from "../../components/page-header";
import { TABLE_COLUMNS, TABLE_INVERSION_COLUMNS } from "./constants";
import {
  getSWarningCust,
  getControlinversionSelectiesscaopinfo,
  getControlinversionSelectOEquipType,
  getControlinversionSelectInversionVo
} from "@/api/scada";
import moment from "moment";
export default {
  name: "warning",
  props: {},

  data() {
    const columns = [
      ...TABLE_COLUMNS,
      {
        title: "操作",
        width: 100,
        fixed: "right",
        align: "center",
        render: (h, params) => {
          const { row } = params;
          return h("span", [
            h(
              "Button",
              {
                props: {
                  type: "primary",
                  size: "small"
                },
                nativeOn: {
                  click: () => {
                    this.handleInversion(row);
                  }
                }
              },
              "反演"
            )
          ]);
        }
      }
    ];
    const invesionColumns = [...TABLE_INVERSION_COLUMNS];
    return {
      loading: {
        normal: false,
        search: false,
        reset: false,
        page: false
      },
      options: {
        custs: [],
        equipments: []
      },
      formdata: {
        username: "",
        device: "",
        deviceid: "",
        time: "",
        account: "",
        page: 1,
        pageSize: 10,
        total: 0
      },
      columns,
      data: [],
      inversion: {
        modal: false,
        page: 1,
        pageSize: 10,
        total: 0,
        columns: invesionColumns,
        data: []
      }
    };
  },
  computed: {
    tableLoading() {
      const {
        loading: { normal, search, reset, page }
      } = this;
      return normal || search || reset || page;
    }
  },
  created() {},
  mounted() {
    this.getSWarningCust();
    this.getControlinversionSelectOEquipType();
    this.getControlinversionSelectiesscaopinfo();
  },
  watch: {},
  methods: {
    handleQuery() {
      this.formdata.page = 1;
      this.getControlinversionSelectiesscaopinfo("search");
    },
    handleReset() {
      this.$refs.form.resetFields();
      this.formdata.page = 1;
      this.getControlinversionSelectiesscaopinfo("reset");
    },
    handlePage(page) {
      this.formdata.page = page;
      this.getControlinversionSelectiesscaopinfo();
    },
    handlePageSize(pageSize) {
      this.formdata.pageSize = pageSize;
      this.getControlinversionSelectiesscaopinfo();
    },
    handleInversion(row) {
      this.inversion.modal = true;
      this.getControlinversionSelectInversionVo(row);
    },
    getSWarningCust() {
      getSWarningCust().then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          this.options.custs = (result || []).map((element) => {
            const { custId, custName } = element;
            return {
              value: custName,
              label: custName
            };
          });
        }
      });
    },
    getControlinversionSelectOEquipType() {
      getControlinversionSelectOEquipType().then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          this.options.equipments = (result || []).map((element) => {
            const { equipmentid, equipmenttype } = element;
            return {
              value: equipmenttype,
              label: equipmenttype
            };
          });
        }
      });
    },
    getControlinversionSelectiesscaopinfo(loadingType = "normal") {
      this.loading[loadingType] = true;
      const {
        formdata: {
          username,
          device,
          deviceid,
          account,
          time,
          page: currentPage,
          pageSize
        }
      } = this;
      let [begintime, endtime] = time;
      begintime = begintime ? moment(begintime).format("yyyy-MM-DD") : "";
      endtime = endtime ? moment(endtime).format("yyyy-MM-DD") : "";
      const params = {
        username,
        device,
        deviceid,
        account,
        begintime,
        endtime,
        currentPage,
        pageSize
      };
      getControlinversionSelectiesscaopinfo(params)
        .then((ret) => {
          const { code, result } = ret || {};
          if (code === 200) {
            const { totalNum, custom } = result || {};
            this.formdata.total = totalNum || 0;
            this.data = custom || [];
          }
        })
        .finally(() => {
          this.loading[loadingType] = false;
        });
    },
    getControlinversionSelectInversionVo(data) {
      const { deviceid: devid } = data;
      getControlinversionSelectInversionVo({ devid }).then((ret) => {
        const { code, result } = ret || {};
        if (code === 200) {
          this.setControlinversionSelectInversionVo(result || []);
        }
      });
    },
    setControlinversionSelectInversionVo(data) {
      const newData = [...data];
      this.inversion.data = [];
      this.inversion.data.push(newData.shift());
      const timer = setInterval(() => {
        if (newData.length === 0) {
          clearInterval(timer);
        } else {
          this.inversion.data.push(newData.shift());
        }
      }, 1000);
    }
  },
  components: { PageHeader }
};
</script>
<style scoped lang="scss">
.bg-style {
  background: url(~@/assets/img/welcome.png) center no-repeat;
  background-size: contain;
  width: 100%;
  height: 100%;
}
.container {
  padding: 20px;
  height: 100%;
  overflow: auto;
}
.container-title {
  padding-left: 10px;
  font-size: 16px;
  color: #333;
  position: relative;
}
.container-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  display: inline-block;
  width: 4px;
  height: 16px;
  background-color: #0893fd;
  line-height: 16px;
}

.container-space > .container-space__child:not(:last-child) {
  margin-right: 20px;
}
.container-space.container-space--vertical
  > .container-space__child:not(:last-child) {
  margin-bottom: 20px;
  margin-right: 0;
}
.container-space--inner {
  padding: 20px;
}

.container-align--center {
  text-align: center;
}
.container-align--right {
  text-align: right;
}
.container-page {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
<style>
.ivu-btn-ghost {
  color: #fff;
  background: transparent;
}
</style>
<style lang="scss" scoped>
/deep/ .ivu-table-tip table tr td {
  text-align: center;
  background-image: url("~@/assets/img/zwsj.png");
  background-position: center 60px;
  background-repeat: no-repeat;
  color: #999;
  height: 369px;
  background-size: 190px;
  span {
    display: none;
  }
}
</style>
